package com.laopan.wxpay.request.close;

import com.laopan.wxpay.WxConstants;
import com.laopan.wxpay.bean.OrderMch;
import com.laopan.wxpay.bean.result.CloseOrderResult;
import com.laopan.wxpay.config.WxPayConfig;
import com.laopan.wxpay.exception.WxPayException;
import com.laopan.wxpay.request.RequestCallback;
import com.laopan.wxpay.request.WxPayRequest;
import org.apache.http.HttpStatus;

/**
 * 关闭订单
 *
 * @author laopan
 * @className OrderClose
 * @date 2021/2/26 15:37
 */

public class OrderClose extends WxPayRequest<OrderMch, CloseOrderResult> {
    public OrderClose(WxPayConfig wxPayConfig) {
        super(wxPayConfig);
    }

    @Override
    protected Class<CloseOrderResult> getResultClass() {
        return CloseOrderResult.class;
    }

    public boolean close(String mchId, String id) throws WxPayException {
        return close(mchId, id, response -> {
            CloseOrderResult result1 = new CloseOrderResult();
            int code = response.getStatusLine().getStatusCode();
            result1.setCode(code);
            if (code == HttpStatus.SC_NO_CONTENT) {
                result1.setClose(true);
            }
            return result1;
        });
    }

    public boolean close(String mchId, String id, RequestCallback<CloseOrderResult> requestCallback) throws WxPayException {
        CloseOrderResult result = closeOrder(mchId, id, requestCallback);
        return result.isClose();
    }

    public CloseOrderResult closeOrder(String mchId, String id, RequestCallback<CloseOrderResult> requestCallback) throws WxPayException {
        OrderMch closeOrder = new OrderMch();
        closeOrder.setMchId(mchId);
        return post(WxConstants.MCH_QUERY_PATH + id + "/close", closeOrder, requestCallback);
    }
}
